/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package towerz.component.panel;

import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import towerz.game.Sprite;
import towerz.game.entity.hut.Hut;
import towerz.game.entity.hut.HutData;
import towerz.game.entity.hut.HutFactory;
import towerz.game.entity.tower.TowerData;
import towerz.game.entity.tower.TowerFactory;
import towerz.game.level.Level;
import towerz.system.BitmapFont;
import towerz.system.StateManager;
import towerz.system.StringManager;
import towerz.system.Utilities;

/**
 *
 * @author LegACy
 */
public class HutPanel extends Panel {
	//Action enumeration
	public static final int ACTION_SELL		= 0;
	public static final int ACTION_TOWER	= 1;
	public static final int ACTION_UPGRADE	= 2;

	public HutPanel(Image image, int x, int y) {
		//Super
		super(image, x, y);

		//Initialize
		m_Current	= 0;
		m_Towers	= null;
		m_Level		= null;

		//Load arrow image
		try {
			m_Arrow = new Sprite(Utilities.PANELARROW_IMAGE, 0, 0, 3, 1);
			m_Arrow.setPosition(m_X + ((m_Background.getWidth() - m_Arrow.getWidth()) / 2), m_Y + 24);
		} catch (IOException ex) {}
	}

	public void show(Hut hut, Level level, int towerBuilt) {
		//Super
		super.show();

		//Save
		m_Hut			= hut;
		m_Level			= level;
		m_TowerBuilt	= towerBuilt;

		//Get allowed tower
		m_Towers	= m_Hut.getData().getTowers(m_Level);
		m_Upgrades	= m_Hut.getData().getAvailableUpgrades(m_Level);

		//Calculate choice limit
		m_Limit = m_Towers.length + m_Upgrades.length;
		
		//Get number of warrios
		String WarriorCount = String.valueOf(m_TowerBuilt) + "/" + String.valueOf(m_Hut.getData().getLimit()) + " " + StringManager.instance().getString("warrior").toLowerCase();
		m_Warriors			= BitmapFont.getFont(0).renderTransparentText(WarriorCount, 0x00000000);
		

		m_StringImg = new Image[7];
		loadImg();
	}
	
	public static void loadText() {
		//Load
		s_Sell				= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(StringManager.instance().getString("sell").toUpperCase(), 0x00000000);
		s_SellDescription0	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(StringManager.instance().getString("sell-description0"), 0x00000000);
		s_SellDescription1	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(StringManager.instance().getString("sell-description1"), 0x00000000);
		s_SellDescription2	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(StringManager.instance().getString("sell-description2"), 0x00000000);
		s_SellDescription3	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(StringManager.instance().getString("sell-description3"), 0x00000000);
		s_UpgradeString		= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(StringManager.instance().getString("upgrade"), 0x00000000);
		s_SummonString		= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(StringManager.instance().getString("summon"), 0x00000000);
	} 

	private void loadImg(){
		//Load Image based on action
		switch (getAction()) {
		case HutPanel.ACTION_TOWER:
			//Get tower
			TowerData SelectedTower = TowerFactory.instance().getTowerData(m_Towers[m_Current]);

			//Draw
			m_StringImg[0] = m_Warriors;
			
			m_StringImg[1] = SelectedTower.getImage();
			m_StringImg[2] = s_SummonString;
			m_StringImg[3] = SelectedTower.getNameString();

			m_StringImg[4] = SelectedTower.getDescString();

			String dmg = StringManager.instance().getString("damage") + ": " + String.valueOf(SelectedTower.getBullet().getDamage()) + " + " + m_Hut.getData().getBonusDmg();
			m_StringImg[5] = BitmapFont.getFont(0).renderTransparentText(dmg, 0x00000000);

			m_StringImg[6] = SelectedTower.getCostString();

			//String atk = String.valueOf((float)1000/SelectedTower.getReload()) + " Atk // sec";
			//g.drawImage(BitmapFont.getFont(0).renderTransparentText(atk, 0x00000000), m_X + 8, m_Y + SelectedTower.getImage().getHeight() + 72, Graphics.LEFT | Graphics.TOP);

			break;
		case HutPanel.ACTION_UPGRADE:
			//Get upgrade
			HutData SelectedUpgrade = HutFactory.instance().getHutData(m_Upgrades[m_Current - m_Towers.length]);

			//Draw
			m_StringImg[0] = SelectedUpgrade.getImage();
			m_StringImg[1] = s_UpgradeString;
			m_StringImg[2] = SelectedUpgrade.getNameString();
			m_StringImg[3] = SelectedUpgrade.getDescString();

			if(SelectedUpgrade.getBonusDmg() > 0)
				m_StringImg[4] = BitmapFont.getFont(0).renderTransparentText(StringManager.instance().getString("damage") + " +" + SelectedUpgrade.getBonusDmg(), 0x00000000);

			if(SelectedUpgrade.getBonusSpd() < 1.00f)
				m_StringImg[5] = BitmapFont.getFont(0).renderTransparentText(StringManager.instance().getString("speed") + " " + (1/SelectedUpgrade.getBonusSpd())*100 + "%", 0x00000000);

			String cost = SelectedUpgrade.getCost() + " " + StringManager.instance().getString("culture");
			m_StringImg[6] = BitmapFont.getFont(0).renderTransparentText(cost, 0x00000000);

			break;
		case HutPanel.ACTION_SELL:
			//Sell?
			m_StringImg[0] = s_Sell;
			m_StringImg[1] = s_SellDescription0;
			m_StringImg[2] = s_SellDescription1;
			m_StringImg[3] = s_SellDescription2;
			m_StringImg[4] = s_SellDescription3;

			break;
		}
	}

	public void hide() {
		//Super
		super.hide();

		m_StringImg = null;

		//Reset
		m_Current = 0;
	}

	public int getAction() {
		//Initialize
		int Action = HutPanel.ACTION_UPGRADE;

		//Check
		if (m_Current < m_Towers.length)	Action = HutPanel.ACTION_TOWER;
		else if (m_Current == m_Limit)		Action = HutPanel.ACTION_SELL;

		//Return
		return Action;
	}

	public TowerData getSelectedTower() {
		//Initialize
		TowerData Selected = null;

		//Check current action
		if (getAction() == HutPanel.ACTION_TOWER) {
			//Check gold
			Selected = TowerFactory.instance().getTowerData(m_Towers[m_Current]);
			if (m_Gold < Selected.getCost()) Selected = null;
		}

		//Return
		return Selected;
	}

	public HutData getSelectedUpgrade() {
		//Initialize
		HutData Selected = null;

		//Check action
		if (getAction() == HutPanel.ACTION_UPGRADE) {
			//Check gold
			Selected = HutFactory.instance().getHutData(m_Upgrades[m_Current - m_Towers.length]);
			if (m_Gold < Selected.getCost()) Selected = null;
		}

		//REturn
		return Selected;
	}

	public int getTowerBuilt(){
		return m_TowerBuilt;
	}

	public void update(int keys, int gold, long time) {
		//Super
		super.update(keys, gold, time);

		//Reset arrow
		if (m_Time <= 0) m_Arrow.setFrame(0);

		//Check keys
		if ((keys & GameCanvas.LEFT_PRESSED) != 0)	{ //awalnya : UP
			//Previous
            m_Current--;

			//Check selection
			if (m_Current > m_Limit)	m_Current = 0;
			else if (m_Current < 0)		m_Current = m_Limit;

			loadImg();

			//Animate
			m_Time = ANIMATION_DELAY;
			m_Arrow.setFrame(1);
        }

		if ((keys & GameCanvas.RIGHT_PRESSED) != 0)	{ //awalnya : DOWN
			//
            m_Current++;

			//Check selection
			if (m_Current > m_Limit)	m_Current = 0;
			else if (m_Current < 0)		m_Current = m_Limit;

			loadImg();

			//Animate
			m_Time = ANIMATION_DELAY;
			m_Arrow.setFrame(2);
        }
		
		if ((keys & GameCanvas.LEFT_PRESSED) != 0 || (keys & GameCanvas.RIGHT_PRESSED) != 0) {
			//Cursor sound
			StateManager.instance().playSFX("/cursor.wav");
		}
	}

	public void draw(Graphics g) {
		//Super
		if (m_Hut == null || m_StringImg == null) return;
		super.draw(g);

		//Draw arrow
		m_Arrow.draw(g);

		//Draw based on action
		switch (getAction()) {
		case HutPanel.ACTION_TOWER:
			//Get tower
			TowerData SelectedTower = TowerFactory.instance().getTowerData(m_Towers[m_Current]);

			//Draw
			if (m_StringImg[0] != null) g.drawImage(m_StringImg[0], m_X + m_Background.getWidth() - 3, m_Y + 3, Graphics.RIGHT | Graphics.TOP);

			if (m_StringImg[1] != null) g.drawImage(m_StringImg[1], m_X + (m_Background.getWidth() / 2), m_Y + 16, Graphics.HCENTER | Graphics.TOP);
			if (m_StringImg[2] != null) g.drawImage(m_StringImg[2], m_X + (m_Background.getWidth() / 2), m_Y + SelectedTower.getImage().getHeight() + 16, Graphics.HCENTER | Graphics.TOP);
			if (m_StringImg[3] != null) g.drawImage(m_StringImg[3], m_X + (m_Background.getWidth() / 2), m_Y + SelectedTower.getImage().getHeight() + 30, Graphics.HCENTER | Graphics.TOP);

			if(m_StringImg[4] != null)
				g.drawImage(m_StringImg[4], m_X + 8, m_Y + SelectedTower.getImage().getHeight() + 45, Graphics.LEFT | Graphics.TOP);

			if(m_StringImg[5] != null)
				g.drawImage(m_StringImg[5], m_X + 8, m_Y + SelectedTower.getImage().getHeight() + 60, Graphics.LEFT | Graphics.TOP);

			if(m_StringImg[6] != null)
				g.drawImage(m_StringImg[6], m_X + m_Background.getWidth(), m_Y + m_Background.getHeight() - 2, Graphics.RIGHT | Graphics.BOTTOM);

			//String atk = String.valueOf((float)1000/SelectedTower.getReload()) + " Atk // sec";
			//g.drawImage(BitmapFont.getFont(0).renderTransparentText(atk, 0x00000000), m_X + 8, m_Y + SelectedTower.getImage().getHeight() + 72, Graphics.LEFT | Graphics.TOP);

			break;
		case HutPanel.ACTION_UPGRADE:
			//Get upgrade
			HutData SelectedUpgrade = HutFactory.instance().getHutData(m_Upgrades[m_Current - m_Towers.length]);

			//Draw
			if (m_StringImg[0] != null) g.drawImage(m_StringImg[0], m_X + (m_Background.getWidth() / 2), m_Y + 8, Graphics.HCENTER | Graphics.TOP);
			if (m_StringImg[1] != null) g.drawImage(m_StringImg[1], m_X + (m_Background.getWidth() / 2), m_Y + SelectedUpgrade.getImage().getHeight() + 14, Graphics.HCENTER | Graphics.TOP);
			if (m_StringImg[2] != null) g.drawImage(m_StringImg[2], m_X + (m_Background.getWidth() / 2), m_Y + SelectedUpgrade.getImage().getHeight() + 28, Graphics.HCENTER | Graphics.TOP);

			if (m_StringImg[3] != null) g.drawImage(m_StringImg[3], m_X + 8, m_Y + SelectedUpgrade.getImage().getHeight() + 48, Graphics.LEFT | Graphics.TOP);

			if(SelectedUpgrade.getBonusDmg() > 0)
				g.drawImage(m_StringImg[4], m_X + 8, m_Y + SelectedUpgrade.getImage().getHeight() + 68, Graphics.LEFT | Graphics.TOP);

			if(SelectedUpgrade.getBonusSpd() < 1.00f)
				g.drawImage(m_StringImg[5], m_X + 8, m_Y + SelectedUpgrade.getImage().getHeight() + 80, Graphics.LEFT | Graphics.TOP);

			if (m_StringImg[6] != null) g.drawImage(m_StringImg[6], m_X + m_Background.getWidth(), m_Y + m_Background.getHeight() - 2, Graphics.RIGHT | Graphics.BOTTOM);

			break;
		case HutPanel.ACTION_SELL:
			//Sell?
			if (m_StringImg[0] != null) g.drawImage(m_StringImg[0], m_X + (m_Background.getWidth() / 2), m_Y + 20, Graphics.HCENTER | Graphics.TOP);
			if (m_StringImg[1] != null) g.drawImage(m_StringImg[1], m_X + (m_Background.getWidth() / 2), m_Y + 72, Graphics.HCENTER | Graphics.TOP);
			if (m_StringImg[2] != null) g.drawImage(m_StringImg[2], m_X + (m_Background.getWidth() / 2), m_Y + 86, Graphics.HCENTER | Graphics.TOP);
			if (m_StringImg[3] != null) g.drawImage(m_StringImg[3], m_X + (m_Background.getWidth() / 2), m_Y + 100, Graphics.HCENTER | Graphics.TOP);
			if (m_StringImg[4] != null) g.drawImage(m_StringImg[4], m_X + (m_Background.getWidth() / 2), m_Y + 114, Graphics.HCENTER | Graphics.TOP);

			break;
		}
	}
	
	protected static Image s_Sell;
	protected static Image s_SellDescription0;
	protected static Image s_SellDescription1;
	protected static Image s_SellDescription2;
	protected static Image s_SellDescription3;
	protected static Image s_UpgradeString;
	protected static Image s_SummonString;

	//Members
	protected Hut		m_Hut;
	protected int		m_Limit;
	protected int		m_Current;
	protected int		m_TowerBuilt; //Sudah nge-build brapa tower
	protected int[]		m_Upgrades;
	protected int[]		m_Towers;
	protected Level		m_Level;
	protected Sprite	m_Arrow;

	protected Image		m_Warriors;
	protected Image[]	m_StringImg;
}
